package com.peng.sms.controller;

import com.peng.sms.service.SmsService;
import com.peng.sms.utils.RandomUtil;
import com.peng.utils.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.concurrent.TimeUnit;

@Slf4j
@RestController
@RequestMapping("/sms/message")
@CrossOrigin
public class SmsController {
    @Autowired
    private SmsService smsService;
    @Autowired
    private RedisTemplate<String, String> redisTemplate;

    //发送短信服务
    @PostMapping("/send/{phone}")
    public Result sendSms(@PathVariable String phone){
        String code = (String) redisTemplate.opsForValue().get(phone);
        if (!StringUtils.isEmpty(code)){
            return Result.ok();
        }
        code = RandomUtil.getSixBitRandom();
//        boolean flag=smsService.sendSms(phone,code);
        log.info("短信验证码-----》{}",code);
        if (true){
            redisTemplate.opsForValue().set(phone,code,5, TimeUnit.MINUTES);
            return Result.ok();
        }
        return Result.error();
    }
}
